package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.dto.LikesBehaviorDto;
import com.heima.behavior.dto.UnLikesBehaviorDto;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.behavior.entity.UpdateArticleMessage;
import com.heima.behavior.service.IlikeService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.entity.User;
import com.heima.common.util.AppThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.util.Date;
import java.util.List;

@Service
public class LikeServiceImpl implements IlikeService {
    @Autowired
    private MongoTemplate template;

    /**
     * 点赞行为
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
//       创建点赞对象
        ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
        User user = AppThreadLocal.get();
        if (user != null) {
            apLikesBehavior.setUserId(user.getUserId());
        }else {
            apLikesBehavior.setEquipmentId(dto.getEquipmentId());
        }
        apLikesBehavior.setArticleId(dto.getArticleId());
        apLikesBehavior.setType(dto.getType());
        apLikesBehavior.setOperation(dto.getOperation());
        apLikesBehavior.setCreatedTime(new Date());
//        搜索对象是否存在
        Query query = new Query();
        query.addCriteria(Criteria.where("articleId").is(dto.getArticleId()));
        if (user != null){
            query.addCriteria(Criteria.where("userId").is(user.getUserId()));
        }else {
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));

        }

        ApLikesBehavior one = template.findOne(query, ApLikesBehavior.class);
//        判断是否存在
            if (one == null){
                template.save(apLikesBehavior);
            }else {
                if (dto.getOperation() != one.getOperation()){
                    one.setOperation(dto.getOperation());
                    template.save(one);
                }
            }
            sendMessage(dto);
        return ResponseResult.okResult();
    }

    /**
     * 关注查询行为
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<ApLikesBehavior> getLikes(LikesBehaviorDto dto) {
        Query query = new Query();

        query.addCriteria(Criteria.where("articleId").is(dto.getArticleId()));
        if (dto.getUserId() == null){
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }else {
            query.addCriteria(Criteria.where("userId").is(dto.getUserId()));
        }
        ApLikesBehavior one = template.findOne(query, ApLikesBehavior.class);
        System.out.println(one);
        return ResponseResult.okResult(one);
    }

    /**
     * 不喜欢行为
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<ApUnlikesBehavior> getUnlike(UnLikesBehaviorDto dto) {
        Query query = new Query();
        query.addCriteria(Criteria.where("articleId").is(dto.getArticleId()));
        User user = AppThreadLocal.get();
        if (user != null){
            query.addCriteria(Criteria.where("userId").is(user.getUserId()));
        }else {
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }
        ApUnlikesBehavior one = template.findOne(query, ApUnlikesBehavior.class);
        return ResponseResult.okResult(one);
    }

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Value("${topic.like_behavior}")
    private String LikeTopic;

    /**
     * kafka发送消息
     * @param dto
     */
    private void sendMessage(LikesBehaviorDto dto) {
        UpdateArticleMessage message = new UpdateArticleMessage();
        message.setType(1);
        message.setArticleId(dto.getArticleId());
        message.setAdd(1);
        if (dto.getOperation() == 1){
            message.setAdd(-1);
        }
        String json = JSON.toJSONString(message);
        kafkaTemplate.send(LikeTopic,json);
    }
}
